package com.Blog.www.service.Impl;

import com.Blog.www.constant.PageConstant;
import com.Blog.www.constant.SqlConstant;
import com.Blog.www.constant.TipsConstant;
import com.Blog.www.dao.Impl.LikeDaoImpl;
import com.Blog.www.dao.LikeDao;
import com.Blog.www.po.UserLike;
import com.Blog.www.service.LikeService;
import com.Blog.www.utils.DataExamine;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户收藏相关实现类
 *
 * @author 君未洋
 */
public class LikeServiceImpl implements LikeService {

    private static LikeDao dao = new LikeDaoImpl();
    private static DataExamine date = new DataExamine();

    @Override
    public Boolean userLike(String uid, String bid, String status, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Boolean flag = false;
        if (date.numberExamine(bid) || date.numberExamine(uid)) {
            req.setAttribute(PageConstant.BLOG_I, TipsConstant.BLOG_OPERATION_G);
            //跳转回原来的页面
        } else {
            if (SqlConstant.OPERATE_A.equals(status)) {
                if (dao.add(uid, bid)) {
                    flag = true;
                }
            } else if (SqlConstant.OPERATE_B.equals(status)) {
                if (dao.delete(uid, bid)) {
                    flag = true;
                }
            }
            //没问题就直接操作楼
            if (flag) {
                req.setAttribute(PageConstant.BLOG_I, TipsConstant.BLOG_OPERATION_H);
            } else {
                req.setAttribute(PageConstant.BLOG_I, TipsConstant.BLOG_OPERATION_H);
            }
            //操作完成后跳转回原来的页面
        }
        req.getRequestDispatcher("/UserBlog/Blog?id=" + bid).forward(req, resp);
        return flag;
    }

    @Override
    public boolean findLike(int uid, int bid) {
        //这里也没什么好做的,主要就是校验看看输入的数据有没有问题
        if (uid == 0 || bid == 0) {
            return false;
        } else {
            return dao.findLike(uid, bid);
        }
    }

    @Override
    public List<Integer> findUserLike(int id) {
        List<UserLike> like = dao.findLike(id);
        if (like == null || like.size() == 0) {
            return null;
        } else {
            List<Integer> bids = new ArrayList<>();
            for (UserLike userLike : like) {
                bids.add(userLike.getBlogid());
            }
            return bids;
        }
    }
}
